package com.xiaotou.domain;

import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;

import javax.imageio.ImageIO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xiaotou.cache.ItemsCache;
import com.xiaotou.common.ImageHandler;
import com.xiaotou.db.pojo.ActionTask;
import com.xiaotou.db.pojo.Item;
import com.xiaotou.db.pojo.WbUser;
import com.xiaotou.weibo.WeiboUser;

@Component
public class Engine {

	private static Logger logger = LoggerFactory.getLogger(Engine.class);

	private RuleParam ruleParam;

	private ItemsCache itemsCache;

	@Autowired
	private Engine(RuleParam ruleParam) {
		this.ruleParam = ruleParam;
	}

	public Thief actThief(WeiboUser weiboUser) {
		return EngineFactory.createThief(this, weiboUser, this.ruleParam);
	}

	public Prey actPrey(WeiboUser weiboUser) {
		return EngineFactory.createPrey(this, weiboUser, this.ruleParam);
	}

	public List<Prey> actPreys(List<WeiboUser> weiboUsers) {
		final List<Prey> preys = new ArrayList<Prey>();
		for (WeiboUser weiboUser : weiboUsers) {
			preys.add(EngineFactory.createPrey(this, weiboUser, this.ruleParam));
		}

		return preys;
	}

	public Partner actPartner(WeiboUser weiboUser) {
		return EngineFactory.createPartner(this, weiboUser, this.ruleParam);
	}

	public Police actPolice(WeiboUser weiboUser) {
		return EngineFactory.createPolice(this, weiboUser, this.ruleParam);
	}

	public Informant actInformant(WeiboUser weiboUser) {
		return EngineFactory.createInformant(this, weiboUser, this.ruleParam);
	}

	public int getPartnersThreshold(Thief thief, Prey prey) {
		return 1;
	}

	public String snapshot(ActionTask actionTask, String basePath) {
		final WbUser preyUser = actionTask.getPrey();
		Image imgA;
		try {
			imgA = ImageIO.read(new URL(actionTask.getThief()
					.getProfileImageUrl()));
			Image imgB = ImageIO.read(new URL(preyUser.getProfileImageUrl()));
			Image imgC = ImageIO.read(new File(basePath + "/img/cbg.jpg"));
			String snapshot = "/img/cthief" + actionTask.getThief().getId()
					+ UUID.randomUUID() + ".jpg";
			ImageHandler.compile(basePath + snapshot, imgA, imgB, imgC);
			actionTask.setSnapshot(snapshot);
		} catch (MalformedURLException e) {
			logger.error(e.getMessage(), e);
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
		}
		return actionTask.getSnapshot();
	}

	public WealthLevel getWealthLevel(Prey prey) {
		final WeiboUser actor = prey.getActor();
		if (actor.getFriendsCount() > ruleParam.getRichThreshold()) {
			return WealthLevel.Rich;
		} else if (actor.getFriendsCount() > ruleParam.getMiddleThreshold()) {
			return WealthLevel.Middle;
		} else {
			return WealthLevel.Poor;
		}
	}

	public SkillLevel getSkillLevel(Role role) {
		final WeiboUser actor = role.getActor();
		if (actor.getFriendsCount() > ruleParam.getRichThreshold()) {
			return SkillLevel.Beginner;
		} else if (actor.getFriendsCount() > ruleParam.getMiddleThreshold()) {
			return SkillLevel.Expert;
		} else {
			return SkillLevel.Beginner;
		}
	}

	public int getItemAmount(Prey prey, String ItemName) {
		return 0;
	}

	public Collection<Item> getAllItems() {
		return this.itemsCache.getItems();
	}

	public static Engine newInstance(RuleParam ruleParam) {
		return new Engine(ruleParam);
	}

	public void setRuleParam(RuleParam ruleParam) {
		this.ruleParam = ruleParam;
	}

	public void setItemsCache(ItemsCache itemsCache) {
		this.itemsCache = itemsCache;
	}
}
